/**
 *  EKology - An Ecosystem Simulation Framework
 *  Copyright (C) 2008  Aarón Tavío - aaron.tavio at gmail.com - ULPGC
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

package fungusEater.ekosystem;

import fungusEater.Config;
import fungusEater.FungusEaterHelper;
import fungusEater.datatypes.*;

import net.ekology.graphics.EKPainter;
import net.ekology.ekosystem.AbioticAgent;
import net.ekology.ekosystem.datatypes.*;
import net.ekology.core.datatypes.Attribute;

import java.awt.*;
import java.awt.geom.*;
import java.io.*;


/**
 * @author Aarón Tavío - aaron.tavio at gmail.com
 * @version 1.0.0 - 20081019-1700
 */
public class Ore extends AbioticAgent implements Serializable {
    // Attributes
    private Taros oTaros;
    
    private Path2D.Double oStar;
    transient private BasicStroke oBorderStroke;
    private Color oBodyDrawColor;
    private Color oBodyFillColor;
    
    private double dWeight;
    private OreType oType;
    
    
    
    ///////////////////////////////////
    // Initialization methods
    ///////////////////////////////////
    public Ore() {
        oStar = new Path2D.Double();
        oStar.moveTo(0, 9);
        oStar.lineTo(2.5, 5);
        oStar.lineTo(8,5);
        oStar.lineTo(5,0);
        oStar.lineTo(8,-5);
        oStar.lineTo(2.5,-5);
        oStar.lineTo(0, -9);
        oStar.lineTo(-2.5,-5);
        oStar.lineTo(-8,-5);
        oStar.lineTo(-5,0);
        oStar.lineTo(-8,5);
        oStar.lineTo(-2.5,5);
        oStar.closePath();
        
        oBorderStroke = new BasicStroke(1f);
        
        oBodyFillColor = new Color(0f, 0.5f, 1f);

    }
    
    
    @Override
    public void initialize() {
        System.out.printf("Ore.java->initialize()\n"); // DEBUG
        oTaros = (Taros) super.oBiotope;
        Space oSpace = oTaros.getSpace();
        
        setID(FungusEaterHelper.generateID());
        
        /* DEBUG BEGIN
        dWeight = 20;
        oType = OreType.A_TYPE;
        
        oBodyDrawColor = Config.A_TYPE_ORE_COLOR;
        
        setPosition(new EKVector(-500,0));
        setOrientation(new EKVector(1,1));
        DEBUG END */
        
        dWeight = FungusEaterHelper.getRandomValue(Config.O_WEIGHT_INIT_MAX,Config.O_WEIGHT_INIT_MIN); // Random x value where: 0.01 <= x < 0.2
        oType = FungusEaterHelper.getRandomOreType();
        
        oBodyDrawColor = getDrawColor(oType);
        
        setPosition(FungusEaterHelper.getRandomPosition(oSpace.getDimension(0),oSpace.getDimension(1)));
        setOrientation(FungusEaterHelper.getRandomPosition(oSpace.getDimension(0),oSpace.getDimension(1)));
        
        printState();
    }
    
    private Color getDrawColor(OreType oType) {
        if (oType == OreType.A_TYPE) return Config.A_TYPE_ORE_COLOR;
        else if (oType == OreType.B_TYPE) return Config.B_TYPE_ORE_COLOR;
        else return Config.C_TYPE_ORE_COLOR;
    }
    
    
    
    ///////////////////////////////////
    // Overriden methods
    ///////////////////////////////////
    @Override
    public String[] getAttributeNames() {
        String[] aResult = {"oType","dWeight"};
        
        return aResult;
    }
    
    
    @Override
    public Attribute getAttribute(String sName) {
        Attribute oResult = null;
        
        if (sName.equals("oType")) oResult = new Attribute<OreType>("oType",oType);
        else if (sName.equals("dWeight")) oResult = new Attribute<Double>("dWeight",dWeight);
        
        return oResult;
    }
    
    
    @Override
    public void setAttribute(String sName, String sValue) {
        if (sName.equals("oType")) {
            oType = OreType.valueOf(sValue.toUpperCase());
            oBodyDrawColor = getDrawColor(oType);
        }
        else if (sName.equals("dWeight")) dWeight = Double.parseDouble(sValue);
    }
    
    
    
    public void draw(Graphics2D g2) {
        Path2D.Double oBody;
        double dXPos,dYPos,dRotation,dScale;
        
        dXPos = getPosition().getComponent(0);
        dYPos = getPosition().getComponent(1);
        dRotation = getOrientation().getDirection();
        dScale = dWeight/4;
        
        oBody = (Path2D.Double) oStar.clone();
        oBody.transform(EKPainter.getTransform(dXPos, dYPos, dRotation, dScale));
        
        g2.setStroke(oBorderStroke);
        g2.setColor(oBodyDrawColor);
        g2.draw(oBody);
        g2.setColor(oBodyFillColor);
        g2.fill(oBody);
    }
    
    
    
    ///////////////////////////////////
    // Other methods
    ///////////////////////////////////
    public double loadOre(double dAmount) {
        double dLoadedOre;
        
        if (dAmount > dWeight) dLoadedOre = dWeight;
        else dLoadedOre = dAmount;
        
        dWeight = dWeight - dLoadedOre;
            
        System.out.printf("Ore.java->loadOre()\n"); // DEBUG
        printState();
        return dLoadedOre;
    }
    
    public double getWeight() {
        return dWeight;
    }
    
    
    public OreType getType() {
        return oType;
    }
    
    public void printState() {
        System.out.printf("Ore=[id: '%s', oType:%s; , dWeight: '%.3f']\n",getID(),oType,dWeight);
    }
    
    private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
        in.defaultReadObject();
        oBorderStroke = new BasicStroke(2f);
    }

}
